package cn.jee.service;

import cn.jee.cache.impl.RedisCache;
import cn.jee.dao.StuDao;
import cn.jee.domain.Stu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class StuService {
  @Autowired
  StuDao dao;
  @Autowired
  RedisCache cache;
  public List<Stu> findAll(){
    if(cache.exists("stu:all")){    //有缓存,从redis返回
      log.info("缓存命中");
      Optional<List<Stu>> res=cache.get("stu:all");
      if(res.isEmpty()) return List.of();
      else return res.get();
    }else{  //没有缓存,查数据库，放在缓存中，返回
      log.info("缓存未命中");
      var res=dao.findAl();
      cache.put("stu:all",res);
      return res;
    }
  }
  public void update(){
    cache.clear("stu");
    //update
  }
}
